home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / nt / emacssrc.zip / EMACSSRC.TAR / emacs-19.17 / lisp / view.el < prev    next >
Lisp/Scheme  |  1993-07-23  |  17KB  |  444 lines

  1. ;;; view.el --- peruse file or buffer without editing.
  2.  
  3. ;; Copyright (C) 1985, 1989 Free Software Foundation, Inc.
  4.  
  5. ;; Author: K. Shane Hartman
  6. ;; Maintainer: FSF
  7.  
  8. ;; This file is part of GNU Emacs.
  9.  
  10. ;; GNU Emacs is free software; you can redistribute it and/or modify
  11. ;; it under the terms of the GNU General Public License as published by
  12. ;; the Free Software Foundation; either version 2, or (at your option)
  13. ;; any later version.
  14.  
  15. ;; GNU Emacs is distributed in the hope that it will be useful,
  16. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  18. ;; GNU General Public License for more details.
  19.  
  20. ;; You should have received a copy of the GNU General Public License
  21. ;; along with GNU Emacs; see the file COPYING.  If not, write to
  22. ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  23.  
  24. ;;; Commentary:
  25.  
  26. ;; This package provides the `view' major mode documented in the Emacs
  27. ;; user's manual.
  28.  
  29. ;;; Code:
  30.  
  31. (defvar view-mode-map nil)
  32. (if view-mode-map
  33.     nil
  34.   (setq view-mode-map (make-keymap))
  35.   (fillarray (nth 1 view-mode-map) 'View-undefined)
  36.   (define-key view-mode-map "\C-c" 'view-exit)
  37.   (define-key view-mode-map "\C-z" 'suspend-emacs)
  38.   (define-key view-mode-map "q" 'view-exit)
  39.   (define-key view-mode-map "-" 'negative-argument)
  40.   (define-key view-mode-map "0" 'digit-argument)
  41.   (define-key view-mode-map "1" 'digit-argument)
  42.   (define-key view-mode-map "2" 'digit-argument)
  43.   (define-key view-mode-map "3" 'digit-argument)
  44.   (define-key view-mode-map "4" 'digit-argument)
  45.   (define-key view-mode-map "5" 'digit-argument)
  46.   (define-key view-mode-map "6" 'digit-argument)
  47.   (define-key view-mode-map "7" 'digit-argument)
  48.   (define-key view-mode-map "8" 'digit-argument)
  49.   (define-key view-mode-map "9" 'digit-argument)
  50.   (define-key view-mode-map "\C-u" 'universal-argument)
  51.   (define-key view-mode-map "\e" nil)
  52.   (define-key view-mode-map "\C-x" 'Control-X-prefix)
  53.   (define-key view-mode-map "\e-" 'negative-argument)
  54.   (define-key view-mode-map "\e0" 'digit-argument)
  55.   (define-key view-mode-map "\e1" 'digit-argument)
  56.   (define-key view-mode-map "\e2" 'digit-argument)
  57.   (define-key view-mode-map "\e3" 'digit-argument)
  58.   (define-key view-mode-map "\e4" 'digit-argument)
  59.   (define-key view-mode-map "\e5" 'digit-argument)
  60.   (define-key view-mode-map "\e6" 'digit-argument)
  61.   (define-key view-mode-map "\e7" 'digit-argument)
  62.   (define-key view-mode-map "\e8" 'digit-argument)
  63.   (define-key view-mode-map "\e9" 'digit-argument)
  64.   (define-key view-mode-map "<" 'beginning-of-buffer)
  65.   (define-key view-mode-map ">" 'end-of-buffer)
  66.   (define-key view-mode-map "\ev" 'View-scroll-lines-backward)
  67.   (define-key view-mode-map "\C-v" 'View-scroll-lines-forward)
  68.   (define-key view-mode-map " " 'View-scroll-lines-forward)
  69.   (define-key view-mode-map "\177" 'View-scroll-lines-backward)
  70.   (define-key view-mode-map "\n" 'View-scroll-one-more-line)
  71.   (define-key view-mode-map "\r" 'View-scroll-one-more-line)
  72.   (define-key view-mode-map "\C-l" 'recenter)
  73.   (define-key view-mode-map "z" 'View-scroll-lines-forward-set-scroll-size)
  74.   (define-key view-mode-map "g" 'View-goto-line)
  75.   (define-key view-mode-map "=" 'what-line)
  76.   (define-key view-mode-map "." 'set-mark-command)
  77.   (define-key view-mode-map "\C-@" 'set-mark-command)
  78.   (define-key view-mode-map "'" 'View-back-to-mark)
  79.   (define-key view-mode-map "@" 'View-back-to-mark)  
  80.   (define-key view-mode-map "x" 'exchange-point-and-mark)
  81.   (define-key view-mode-map "h" 'Helper-describe-bindings)
  82.   (define-key view-mode-map "?" 'Helper-describe-bindings)
  83.   (define-key view-mode-map (char-to-string help-char) 'Helper-help)
  84.   (define-key view-mode-map "\C-n" 'next-line)
  85.   (define-key view-mode-map "\C-p" 'previous-line)
  86.   (define-key view-mode-map "\C-s" 'isearch-forward)
  87.   (define-key view-mode-map "\C-r" 'isearch-backward)
  88.   (define-key view-mode-map "s" 'isearch-forward)
  89.   (define-key view-mode-map "r" 'isearch-backward)
  90.   (define-key view-mode-map "/" 'View-search-regexp-forward)
  91.   (define-key view-mode-map "\\" 'View-search-regexp-backward)
  92.   ;; This conflicts with the standard binding of isearch-regexp-forward
  93.   (define-key view-mode-map "\e\C-s" 'View-search-regexp-forward)
  94.   (define-key view-mode-map "\e\C-r" 'View-search-regexp-backward)  
  95.   (define-key view-mode-map "n" 'View-search-last-regexp-forward)
  96.   (define-key view-mode-map "p" 'View-search-last-regexp-backward)
  97.   )
  98.  
  99.  
  100. ;;;###autoload
  101. (defun view-file (file-name)
  102.   "View FILE in View mode, returning to previous buffer when done.
  103. The usual Emacs commands are not available; instead,
  104. a special set of commands (mostly letters and punctuation)
  105. are defined for moving around in the buffer.
  106. Space scrolls forward, Delete scrolls backward.
  107. For list of all View commands, type ? or h while viewing.
  108.  
  109. This command runs the normal hook `view-hook'."
  110.   (interactive "fView file: ")
  111.   (let ((old-buf (current-buffer))
  112.     (had-a-buf (get-file-buffer file-name))
  113.     (buf-to-view (find-file-noselect file-name)))
  114.     (switch-to-buffer buf-to-view t)
  115.     (view-mode old-buf
  116.            (and (not had-a-buf) (not (buffer-modified-p buf-to-view))
  117.             'kill-buffer))))
  118.  
  119. ;;;###autoload
  120. (defun view-file-other-window (file-name)
  121.   "View FILE in View mode in other window.
  122. Return to previous buffer when done.
  123. The usual Emacs commands are not available; instead,
  124. a special set of commands (mostly letters and punctuation)
  125. are defined for moving around in the buffer.
  126. Space scrolls forward, Delete scrolls backward.
  127. For list of all View commands, type ? or h while viewing.
  128.  
  129. This command runs the normal hook `view-hook'."
  130.   (interactive "fView file: ")
  131.   (let ((old-arrangement (current-window-configuration))
  132.     (had-a-buf (get-file-buffer file-name))
  133.     (buf-to-view (find-file-noselect file-name)))
  134.     (switch-to-buffer-other-window buf-to-view)
  135.     (view-mode old-arrangement
  136.            (and (not had-a-buf) (not (buffer-modified-p buf-to-view))
  137.             'kill-buffer))))
  138.  
  139. ;;;###autoload
  140. (defun view-buffer (buffer-name)
  141.   "View BUFFER in View mode, returning to previous buffer when done.
  142. The usual Emacs commands are not available; instead,
  143. a special set of commands (mostly letters and punctuation)
  144. are defined for moving around in the buffer.
  145. Space scrolls forward, Delete scrolls backward.
  146. For list of all View commands, type ? or h while viewing.
  147.  
  148. This command runs the normal hook `view-hook'."
  149.   (interactive "bView buffer: ")
  150.   (let ((old-buf (current-buffer)))
  151.     (switch-to-buffer buffer-name t)
  152.     (view-mode old-buf nil)))
  153.  
  154. ;;;###autoload
  155. (defun view-buffer-other-window (buffer-name not-return)
  156.   "View BUFFER in View mode in another window,
  157. returning to original buffer when done *only* if 
  158. prefix argument NOT-RETURN is nil (which is the default).
  159.  
  160. The usual Emacs commands are not available in View mode; instead,
  161. a special set of commands (mostly letters and punctuation)
  162. are defined for moving around in the buffer.
  163. Space scrolls forward, Delete scrolls backward.
  164. For list of all View commands, type ? or h while viewing.
  165.  
  166. This command runs the normal hook `view-hook'."
  167.   (interactive "bView buffer:\nP")
  168.   (let ((return-to (and not-return (current-window-configuration))))
  169.     (switch-to-buffer-other-window buffer-name)
  170.     (view-mode return-to)))
  171.  
  172. ;;;###autoload
  173. (defun view-mode (&optional prev-buffer action)
  174.   "Major mode for viewing text but not editing it.
  175. Letters do not insert themselves.  Instead these commands are provided.
  176. Most commands take prefix arguments.  Commands dealing with lines
  177. default to \"scroll size\" lines (initially size of window).
  178. Search commands default to a repeat count of one.
  179. M-< or <    move to beginning of buffer.
  180. M-> or >    move to end of buffer.
  181. C-v or Space    scroll forward lines.
  182. M-v or DEL    scroll backward lines.
  183. CR or LF    scroll forward one line (backward with prefix argument).
  184. z        like Space except set number of lines for further
  185.            scrolling commands to scroll by.
  186. C-u and Digits    provide prefix arguments.  `-' denotes negative argument.
  187. =        prints the current line number.
  188. g        goes to line given by prefix argument.
  189. / or M-C-s    searches forward for regular expression
  190. \\ or M-C-r    searches backward for regular expression.
  191. n        searches forward for last regular expression.
  192. p        searches backward for last regular expression.
  193. C-@ or .    set the mark.
  194. x        exchanges point and mark.
  195. C-s or s    do forward incremental search.
  196. C-r or r    do reverse incremental search.
  197. @ or '        return to mark and pops mark ring.
  198.           Mark ring is pushed at start of every
  199.           successful search and when jump to line to occurs.
  200.           The mark is set on jump to buffer start or end.
  201. ? or h        provide help message (list of commands).
  202. \\[Helper-help]        provides help (list of commands or description of a command).
  203. C-n        moves down lines vertically.
  204. C-p        moves upward lines vertically.
  205. C-l        recenters the screen.
  206. q or C-c    exit view-mode and return to previous buffer.
  207.  
  208. Entry to this mode runs the normal hook `view-hook'.
  209.  
  210. \\{view-mode-map}"
  211. ;  Not interactive because dangerous things happen
  212. ;  if you call it without passing a buffer as argument
  213. ;  and they are not easy to fix.
  214. ;  (interactive)
  215.   (make-local-variable 'view-old-mode-line-buffer-identification)
  216.   (setq view-old-mode-line-buffer-identification
  217.     mode-line-buffer-identification)
  218.   (make-local-variable 'view-old-buffer-read-only)
  219.   (setq view-old-buffer-read-only buffer-read-only)
  220.   (make-local-variable 'view-old-mode-name)
  221.   (setq view-old-mode-name mode-name)
  222.   (make-local-variable 'view-old-major-mode)
  223.   (setq view-old-major-mode major-mode)
  224.   (make-local-variable 'view-old-local-map)
  225.   (setq view-old-local-map (current-local-map))
  226.   (make-local-variable 'view-old-Helper-return-blurb)
  227.   (setq view-old-Helper-return-blurb
  228.     (and (boundp 'Helper-return-blurb) Helper-return-blurb))
  229.  
  230.   (setq buffer-read-only t)
  231.   (setq mode-line-buffer-identification
  232.     (list
  233.      (if (buffer-file-name)
  234.          "Viewing %f"
  235.        "Viewing %b")))
  236.   (setq mode-name "View")
  237.   (setq major-mode 'view-mode)
  238.   (setq Helper-return-blurb
  239.     (format "continue viewing %s"
  240.         (if (buffer-file-name)
  241.             (file-name-nondirectory (buffer-file-name))
  242.             (buffer-name))))
  243.  
  244.   (make-local-variable 'view-exit-action)
  245.   (setq view-exit-action action)
  246.   (make-local-variable 'view-return-here)
  247.   (setq view-return-here prev-buffer)
  248.   (make-local-variable 'view-exit-position)
  249.   (setq view-exit-position (point-marker))
  250.  
  251.   (make-local-variable 'view-scroll-size)
  252.   (setq view-scroll-size nil)
  253.   (make-local-variable 'view-last-regexp)
  254.   (setq view-last-regexp nil)
  255.  
  256.   (beginning-of-line)
  257.   (setq goal-column nil)
  258.  
  259.   (use-local-map view-mode-map)
  260.   (run-hooks 'view-hook)
  261.   (view-helpful-message))
  262.  
  263.  
  264. (defun view-exit ()
  265.   "Exit from view-mode.
  266. If you viewed an existing buffer, that buffer returns to its previous mode.
  267. If you viewed a file that was not present in Emacs, its buffer is killed."
  268.   (interactive)
  269.   (setq mode-line-buffer-identification
  270.     view-old-mode-line-buffer-identification)
  271.   (setq major-mode view-old-major-mode)
  272.   (setq mode-name view-old-mode-name)
  273.   (use-local-map view-old-local-map)
  274.   (setq buffer-read-only view-old-buffer-read-only)
  275.  
  276.   (goto-char view-exit-position)
  277.   (set-marker view-exit-position nil)
  278.  
  279.   ;; Now do something to the buffer that we were viewing
  280.   ;; (such as kill it).
  281.   (let ((viewed-buffer (current-buffer))
  282.     (action view-exit-action))
  283.     (cond
  284.      ((bufferp view-return-here)
  285.       (switch-to-buffer view-return-here))
  286.      ((window-configuration-p view-return-here)
  287.       (set-window-configuration view-return-here)))
  288.     (if action (funcall action viewed-buffer))))
  289.  
  290. (defun view-helpful-message ()
  291.   (message
  292.      (substitute-command-keys
  293.       "Type \\[Helper-help] for help, \\[Helper-describe-bindings] for commands, \\[view-exit] to quit.")))
  294.  
  295. (defun View-undefined ()
  296.   (interactive)
  297.   (ding)
  298.   (view-helpful-message))
  299.  
  300. (defun view-window-size () (1- (window-height)))
  301.  
  302. (defun view-scroll-size ()
  303.   (min (view-window-size) (or view-scroll-size (view-window-size))))
  304.  
  305. (defvar view-hook nil
  306.   "Normal hook run when starting to view a buffer or file.")
  307.  
  308. ;(defun view-last-command (&optional who what)
  309. ;  (setq view-last-command-entry this-command)
  310. ;  (setq view-last-command who)
  311. ;  (setq view-last-command-argument what))
  312.  
  313. ;(defun View-repeat-last-command ()
  314. ;  "Repeat last command issued in View mode."
  315. ;  (interactive)
  316. ;  (if (and view-last-command
  317. ;       (eq view-last-command-entry last-command))
  318. ;      (funcall view-last-command view-last-command-argument))
  319. ;  (setq this-command view-last-command-entry))
  320.  
  321. (defun View-goto-line (&optional line)
  322.   "Move to line LINE in View mode.
  323. Display is centered at LINE.  Sets mark at starting position and pushes
  324. mark ring."
  325.   (interactive "p")
  326.   (push-mark)
  327.   (goto-line (or line 1))
  328.   (recenter (/ (view-window-size) 2)))
  329.  
  330. (defun View-scroll-lines-forward (&optional lines)
  331.   "Scroll forward in View mode, or exit if end of text is visible.
  332. No arg means whole window full, or number of lines set by \\[View-scroll-lines-forward-set-scroll-size].
  333. Arg is number of lines to scroll."
  334.   (interactive "P")
  335.   (if (pos-visible-in-window-p (point-max))
  336.       (view-exit)
  337.     (setq lines
  338.       (if lines (prefix-numeric-value lines)
  339.         (view-scroll-size)))
  340.     ;; (view-last-command 'View-scroll-lines-forward lines)
  341.     (if (>= lines (view-window-size))
  342.     (scroll-up nil)
  343.       (if (>= (- lines) (view-window-size))
  344.       (scroll-down nil)
  345.     (scroll-up lines)))
  346.     (cond ((pos-visible-in-window-p (point-max))
  347.        (goto-char (point-max))
  348.        (recenter -1)
  349.        (message (substitute-command-keys
  350.              "End.  Type \\[view-exit] to quit viewing."))))
  351.     (move-to-window-line -1)
  352.     (beginning-of-line)))
  353.  
  354. (defun View-scroll-lines-forward-set-scroll-size (&optional lines)
  355.   "Scroll forward LINES lines in View mode, setting the \"scroll size\".
  356. This is the number of lines which \\[View-scroll-lines-forward] and \\[View-scroll-lines-backward] scroll by default.
  357. The absolute value of LINES is used, so this command can be used to scroll
  358. backwards (but \"scroll size\" is always positive).  If LINES is greater than
  359. window height or omitted, then window height is assumed.  If LINES is less
  360. than window height then scrolling context is provided from previous screen."
  361.   (interactive "P")
  362.   (if (not lines)
  363.       (setq view-scroll-size (view-window-size))
  364.     (setq lines (prefix-numeric-value lines))
  365.     (setq view-scroll-size
  366.       (min (if (> lines 0) lines (- lines)) (view-window-size))))
  367.   (View-scroll-lines-forward lines))
  368.  
  369. (defun View-scroll-one-more-line (&optional arg)
  370.   "Scroll one more line up in View mode.
  371. With ARG scroll one line down."
  372.   (interactive "P")
  373.   (View-scroll-lines-forward (if (not arg) 1 -1)))
  374.  
  375. (defun View-scroll-lines-backward (&optional lines)
  376.   "Scroll backward in View mode.
  377. No arg means whole window full, or number of lines set by \\[View-scroll-lines-forward-set-scroll-size].
  378. Arg is number of lines to scroll."
  379.   (interactive "P")
  380.   (View-scroll-lines-forward (if lines
  381.                  (- (prefix-numeric-value lines))
  382.                    (- (view-scroll-size)))))
  383.   
  384. (defun View-search-regexp-forward (n regexp)
  385.   "Search forward for Nth occurrence of REGEXP.
  386. Displays line found at center of window.  REGEXP is remembered for
  387. searching with \\[View-search-last-regexp-forward] and \\[View-search-last-regexp-backward].  Sets mark at starting position and pushes mark ring."
  388.   (interactive "p\nsSearch forward (regexp): ")
  389.   (if (> (length regexp) 0)
  390.       (progn
  391.        ;(view-last-command 'View-search-last-regexp-forward n)
  392.     (view-search n regexp))))
  393.  
  394. (defun View-search-regexp-backward (n regexp)
  395.   "Search backward from window start for Nth instance of REGEXP.
  396. Displays line found at center of window.  REGEXP is remembered for
  397. searching with \\[View-search-last-regexp-forward] and \\[View-search-last-regexp-backward].  Sets mark at starting position and pushes mark ring."
  398.   (interactive "p\nsSearch backward (regexp): ")
  399.   (View-search-regexp-forward (- n) regexp))
  400.  
  401. (defun View-search-last-regexp-forward (n)
  402.   "Search forward from window end for Nth instance of last regexp.
  403. Displays line found at center of window.  Sets mark at starting position
  404. and pushes mark ring."
  405.   (interactive "p")
  406.   (View-search-regexp-forward n view-last-regexp))
  407.  
  408. (defun View-search-last-regexp-backward (n)
  409.   "Search backward from window start for Nth instance of last regexp.
  410. Displays line found at center of window.  Sets mark at starting position and
  411. pushes mark ring."
  412.   (interactive "p")
  413.   (View-search-regexp-backward n view-last-regexp))
  414.  
  415. (defun View-back-to-mark (&optional ignore)
  416.   "Return to last mark set in View mode, else beginning of file.
  417. Displays line at center of window.  Pops mark ring so successive
  418. invocations return to earlier marks."
  419.   (interactive)
  420.   (goto-char (or (mark) (point-min)))
  421.   (pop-mark)
  422.   (recenter (/ (view-window-size) 2)))
  423.          
  424. (defun view-search (times regexp)
  425.   (setq view-last-regexp regexp)
  426.   (let (where)
  427.     (save-excursion
  428.       (move-to-window-line (if (< times 0) 0 -1))
  429.       (if (re-search-forward regexp nil t times)
  430.       (setq where (point))))
  431.     (if where
  432.     (progn
  433.       (push-mark)
  434.       (goto-char where)
  435.       (beginning-of-line)
  436.       (recenter (/ (view-window-size) 2)))
  437.       (message "Can't find occurrence %d of %s" times regexp)
  438.       (sit-for 4))))
  439.  
  440.  
  441. (provide 'view)
  442.  
  443. ;;; view.el ends here
  444.